﻿#include "datapage.h"
#include "ui_datapage.h"

DataPage::DataPage(OpcUA_QThread * opcUA, QWidget *parent) :
    QWidget(parent),
    myOpcUA(opcUA),
    ui(new Ui::DataPage)
{
    ui->setupUi(this);

    m_customPlot = nullptr;

    //安装事件过滤器监听widget大小变化
    ui->widget->installEventFilter(this);

    //初始化图表
    initChart();

    m_currentType = DataType::Temperature;  //默认显示温度

    //为每种类型添加模拟数据
    addSimulatedData(DataType::Temperature);
    addSimulatedData(DataType::Pressure);
}

DataPage::~DataPage()
{
    delete ui;
}

//图表初始化
void DataPage::initChart()
{
    //创建QCustomPlot实例并绑定到widget
    m_customPlot = new QCustomPlot(ui->widget);
    m_customPlot->setParent(ui->widget);
    m_customPlot->resize(ui->widget->size());

    //初始化温度曲线
    QCPGraph* tempGraph = m_customPlot->addGraph();
    tempGraph->setName(u8"温度曲线");
    QPen tempPen(Qt::red);
    tempPen.setWidth(2);
    tempGraph->setPen(tempPen);
    tempGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 6));
    m_graphs[DataType::Temperature] = tempGraph;

    //初始化压力曲线
    QCPGraph* pressGraph = m_customPlot->addGraph();
    pressGraph->setName(u8"压力曲线");
    QPen pressPen(Qt::blue);
    pressPen.setWidth(2);
    pressGraph->setPen(pressPen);
    pressGraph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssSquare, 6));
    m_graphs[DataType::Pressure] = pressGraph;

    //初始只显示当前类型曲线
    for (auto& graph : m_graphs) {
        graph->setVisible(false);
    }
    m_graphs[m_currentType]->setVisible(true);

    //配置坐标轴
    m_customPlot->xAxis->setLabel(u8"时间（秒）");
    m_customPlot->yAxis->setLabel(u8"温度（℃）");

    //设置坐标轴范围
    m_customPlot->xAxis->setRange(0, 10);
    m_customPlot->yAxis->setRange(0, 100);

    //启用交互功能
    m_customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

    //显示图例
    m_customPlot->legend->setVisible(true);

    //添加网格线
    m_customPlot->xAxis->grid()->setVisible(true);
    m_customPlot->yAxis->grid()->setVisible(true);

    //重绘
    m_customPlot->replot();
}

//添加指定类型的模拟数据
void DataPage::addSimulatedData(DataType type)
{
    //生成10个模拟数据点
    for (int i = 0; i < 10; ++i) {
        double x = i;
        double y;

        if (type == DataType::Temperature) {
            //温度：20-30℃
            y = 20 + QRandomGenerator::global()->bounded(10);
        } else {
            //压力：80-120kPa
            y = 80 + QRandomGenerator::global()->bounded(40);
        }

        addChartData(type, x, y);
    }
}

//添加指定类型的数据点到折线图
void DataPage::addChartData(DataType type, double x, double y)
{
    if (!m_graphs.contains(type))
        return;

    //存储数据到对应类型的容器
    m_xData[type].append(x);
    m_yData[type].append(y);

    //更新对应曲线的数据
    m_graphs[type]->setData(m_xData[type], m_yData[type]);

    //如果是当前显示的类型，更新视图
    if (type == m_currentType)
    {
        //自动调整X轴范围（保留最新10个点）
        if (m_xData[type].size() > 10)
        {
            m_customPlot->xAxis->setRange(
                m_xData[type][m_xData[type].size() - 10],
                m_xData[type].last() + 1
            );
        }
        else
        {
            m_customPlot->xAxis->setRange(0, qMax(10.0, x + 1));
        }

        //自动调整Y轴范围
        m_customPlot->yAxis->rescale(true);

        //重绘
        m_customPlot->replot(QCustomPlot::rpQueuedReplot);
    }
}

//清除指定类型的图表数据
void DataPage::clearChartData(DataType type)
{
    if (m_graphs.contains(type))
    {
        m_xData[type].clear();
        m_yData[type].clear();
        m_graphs[type]->setData(m_xData[type], m_yData[type]);

        if (type == m_currentType)
        {
            m_customPlot->xAxis->setRange(0, 10);
            m_customPlot->yAxis->rescale(true);
            m_customPlot->replot();
        }
    }
}

//切换显示的数据类型
void DataPage::switchDataType(DataType type)
{
    if (m_currentType == type) return;

    //隐藏当前曲线，显示新曲线
    m_graphs[m_currentType]->setVisible(false);
    m_currentType = type;
    m_graphs[m_currentType]->setVisible(true);

    //更新标签
    updateChartLabels(type);

    //调整坐标轴范围
    if (!m_xData[type].isEmpty())
    {
        if (m_xData[type].size() > 10)
        {
            m_customPlot->xAxis->setRange(
                m_xData[type][m_xData[type].size() - 10],
                m_xData[type].last() + 1
            );
        }
        else
        {
            m_customPlot->xAxis->setRange(0, qMax(10.0, m_xData[type].last() + 1));
        }
        m_customPlot->yAxis->rescale(true);
    }
    else
    {
        m_customPlot->xAxis->setRange(0, 10);
        m_customPlot->yAxis->setRange(0, 100);
    }

    m_customPlot->replot();
}

//更新图表标题和轴标签
void DataPage::updateChartLabels(DataType type)
{
    switch (type) {
    case DataType::Temperature:
        m_customPlot->yAxis->setLabel(u8"温度（℃）");
        m_graphs[type]->setName(u8"实时温度曲线");
        break;
    case DataType::Pressure:
        m_customPlot->yAxis->setLabel(u8"压力（kPa）");
        m_graphs[type]->setName(u8"实时压力曲线");
        break;
    //添加更多类型的标签设置
    }
    m_customPlot->replot();
}

//事件过滤器：处理控件大小变化
bool DataPage::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == ui->widget && event->type() == QEvent::Resize)
    {
        if (m_customPlot)
        {
            m_customPlot->resize(ui->widget->size());
            m_customPlot->replot();
        }
    }
    return QWidget::eventFilter(watched, event);
}

void DataPage::on_pushButton_Temperature_clicked()
{
    switchDataType(DataType::Temperature);
    // 此处可添加实际温度数据获取逻辑
    // 示例：addChartData(DataType::Temperature, x, 实际温度值);
}

void DataPage::on_pushButton_Pressure_clicked()
{
    switchDataType(DataType::Pressure);
    // 此处可添加实际压力数据获取逻辑
    // 示例：addChartData(DataType::Pressure, x, 实际压力值);
}
